package com.minshenglife.zanzan.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.dao.ArticleDao;
import com.minshenglife.zanzan.entity.weixin.ArticleTodb;
import com.minshenglife.zanzan.entity.weixin.Material;
import com.minshenglife.zanzan.service.ArticleService;

/**
 * 微信图文service
 * @author jianghaibo
 *
 */
@Component
@Transactional
public class ArticleServiceImpl implements ArticleService{

	@Resource
	ArticleDao articleDao;

	@Override
	public List<ArticleTodb> findByMaterialId(Long id) {
		ArticleTodb articleTodb = new ArticleTodb();
		articleTodb.setMaterialId(id);
		return articleDao.findByExample(articleTodb);
	}

	@Override
	public List<ArticleTodb> findByMaterialId(Long id, String appid) {
		ArticleTodb articleTodb = new ArticleTodb();
		articleTodb.setMaterialId(id);
		articleTodb.setAppid(appid);
		return articleDao.findByExample(articleTodb);
	}

	@Override
	public void addList(List<ArticleTodb> articles, Material material) {
		for (ArticleTodb article : articles) {
			Long id = articleDao.findMaxId();
			if(id != null){
				id += 1l;
			}else{
				id = 1l;
			}
			article.setId(id);
			article.setMaterialId(material.getId());
			articleDao.add(article);
		}
		
	}

	@Override
	public void deleteByMaterialId(Long materialId) {
		ArticleTodb arg0 = new ArticleTodb();
		arg0.setMaterialId(materialId);
		articleDao.deleteByExample(arg0);
		
	}
}
